package com.system.member.service.impl;


import com.system.common.utils.CommonUtil;
import com.system.member.contant.CacheKey;
import com.system.member.exception.SysUserException;
import com.system.member.exception.SysUserExceptionEnum;
import com.system.member.service.NotifyService;
import com.system.member.utils.CheckUtil;
import com.system.member.utils.sendMessage.AliyunUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class NotifyServiceImpl implements NotifyService {

    @Autowired
    private AliyunUtil aliyunUtil;
    /**
     * 验证码的内容
     */
    private static final String CONTENT= "您的验证码是%s,有效时间是10分钟,打死也不要告诉任何人";

    /**
     * 验证码10分钟有效
     */
    private static final int CODE_EXPIRED = 60 * 1000 * 10;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void sendCode(String codeType, String to) {
        String cacheKey = String.format(CacheKey.CHECK_CODE_KEY,codeType,to);

        String cacheValue = redisTemplate.opsForValue().get(cacheKey);

        //如果不为空，则判断是否60秒内重复发送
        if(StringUtils.isNotBlank(cacheValue)){
            long ttl = Long.parseLong(cacheValue.split("_")[1]);
            //当前时间戳-验证码发送时间戳，如果小于60秒，则不给重复发送
            if(CommonUtil.getCurrentTimestamp() - ttl < 1000*60){
                  throw new SysUserException(SysUserExceptionEnum.CAPTCHA_NOT_COMMIT);
            }
        }

        //拼接验证码 2322_324243232424324
        String code = vcode();

        String value = code+"_"+CommonUtil.getCurrentTimestamp();

        redisTemplate.opsForValue().set(cacheKey,value,CODE_EXPIRED, TimeUnit.MILLISECONDS);

        if(CheckUtil.isEmail(to)){
            //邮箱验证码
//          mailService.sendMail(to,SUBJECT,String.format(CONTENT,code));
//           return JsonData.buildSuccess();

        } else if(CheckUtil.isPhone(to)){
            //短信验证码
            aliyunUtil.sendMessage(to,codeType,code);
        }
    }

    /**
     * 生成6位随机数验证码
     * @return
     */
    public  String vcode(){
        String vcode = "1";
        for (int i = 0; i < 5; i++) {
            vcode = vcode + (int)(Math.random() * 9);
        }
        return vcode;
    }

    /**
     * 校验验证码
     * @param
     * @param to
     * @param code
     * @return
     */
    @Override
    public boolean checkCode(String to, String code, String codeType) {
        String cacheKey = String.format(CacheKey.CHECK_CODE_KEY,codeType,to);

        String cacheValue = redisTemplate.opsForValue().get(cacheKey);
        if(StringUtils.isNotBlank(cacheValue)){

            String cacheCode = cacheValue.split("_")[0];
            if(cacheCode.equals(code)){
                //删除验证码
                redisTemplate.delete(cacheKey);
                return true;
            }

        }
        return false;
    }
}
